package padrao2;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;

import br.com.util.Escritor;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;

public class Main {
	public static void main(String[] args) {
		XStream xstream = new XStream() {
			protected MapperWrapper wrapMapper(MapperWrapper next) {  
				return new MapperWrapper(next) {  
					public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn, String fieldName) {  
						return definedIn != Object.class ? super.shouldSerializeMember(definedIn, fieldName) : false;  
					}  
				};  
			}  

		};
		xstream.autodetectAnnotations(true);
		
		Mashup2 m2 = new Mashup2();
		LinkedList<Object> ll = new LinkedList<>();
		
		Retrieve r = new Retrieve();
		r.setEndpoint("http://example.com/");
		r.setUri("http://example.com/brasil");
		ll.add(r);
		
		Retrieve r1 = new Retrieve();
		r1.setEndpoint("http://example.com/");
		r1.setUri("http://example.com/brasil");
		ll.add(r1);
		
		m2.setList(ll);
		
		Remove r2 = new Remove();
		r2.setUri("exemplo.com");
		ll.add(r2);
				
		System.out.println(xstream.toXML(m2));
		
		m2 = (Mashup2) xstream.fromXML(xstream.toXML(m2));
		for(Object o : m2.getList()) {
			if(o instanceof Retrieve) {
				Retrieve r4 = new Retrieve();
				r4.exec();
			} else {
				System.out.println("Comando desconhecido");
			}
		}
		
		
//		Mashup m = new Mashup();
//		Retrieve r = new Retrieve();
//		
//		r.setEndpoint("http://example.com/");
//		r.setUri("http://example.com/brasil");
//		
//		Retrieve r11 = new Retrieve();
//		
//		r11.setEndpoint("http://example.com/");
//		r11.setUri("http://example.com/brasil");
//		
//		Remove r22 = new Remove();
//		r22.setUri("minhaURI");
//		
//		m.setObjects(new Object[] { r, r11, r22 } );
//		
//		Escritor e = new Escritor();
//		e.abreArquivo("./exemplo3.xml", true);
//		e.gravaArquivo(new String[] { xstream.toXML(m) } );
//		e.fechaArquivo();
//		
//		m = (Mashup) xstream.fromXML(new File("./exemplo3.xml"));
//		for(Object o : m.getObjects()) {
//			if(o instanceof Retrieve) {
//				Retrieve r1 = new Retrieve();
//				r1.exec();
//			} else {
//				System.out.println("Comando desconhecido");
//			}
//		}
	}
}
